package com.tech.thread.juc.locks;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
 * ReentrantLock 锁使用方法
 * */
public class ReentrantLockCase {

	public static void main(String[] args) {
		Ticket ticket = new Ticket();
		new Thread(ticket, "一号窗口").start();
		new Thread(ticket, "二号窗口").start();
		new Thread(ticket, "三号窗口").start();
		new Thread(ticket, "四号窗口").start();

	}

}

// 模拟窗口售票
class Ticket implements Runnable {
	Lock lock = new ReentrantLock();

	int tickNum = 10;

	@Override
	public void run() {
		while (true) {
			lock.lock();
			try {
				if (tickNum > 0) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+",售票完成 剩余票数：" + --tickNum);
				}
			} finally {
				lock.unlock();
			}

		}

	}

}
